Ext.define('AmpleHarvest.objects.search.results.panel.cOutput', {
    extend : 'Ext.panel.Panel',
    layout : 'border',
    m_str_SolrType : "reaper",
    m_int_OutputTPL : "",
    stateId : "output_control",
    // cls : "searchWindow",
    requires : [ "AmpleHarvestPHP.objects.search.results.panel.cSort", "AmpleHarvest.DATA.Store.cResults",
    // 'AmpleHarvest.objects.search.window.cGraph_Results',
    'Ext.panel.Panel', 'Ext.Component', 'Ext.XTemplate', 'Ext.layout.container.Border' ],
    uses : [ 'AmpleHarvest.objects.search.results.grid.cGridResults',
        'AmpleHarvest.objects.search.template.grid.cGEStandard' ],

    initComponent : function () {
        var obj_This = this;

        this.m_obj_Params = new Object();
        this.m_str_CurPage = 0;
        this.m_str_TotalPages = 0;
        this.m_str_PerPage = 10;
        this.m_str_Query = "*:*";

        this.m_obj_ResultsStore = Ext.create("AmpleHarvest.DATA.Store.cResults");

        this.m_obj_Mask = new Ext.LoadMask(this, {
            msg : "Searching..."
        });

        this.m_obj_Output = Ext.create('Ext.panel.Panel', {
            layout : {
                type : 'anchor'
            // align : "stretch"
            },
            cls : "searchWindow",
            autoScroll : true,
            region : 'center'
        });

        this.m_obj_Sort = Ext.create("AmpleHarvestPHP.objects.search.results.panel.cSort", {
            region : 'north'
        });

        this.m_obj_Control = Ext.create("Ext.toolbar.Paging", {
            store : this.m_obj_ResultsStore,
            disabled : true,
            height : 25,
            displayInfo : true,
            region : 'south'
        });

        this.items = [ this.m_obj_Sort, this.m_obj_Output, this.m_obj_Control ]

        // Message handlers
        this.addEvents("goodquery");
        this.bubbleEvents = [ "goodquery" ];

        this.m_obj_ResultsStore.on("beforeload", function () {
            this.m_obj_Mask.show();
        }, this);

        this.m_obj_ResultsStore.on("load", function (obj_Store, arr_Rec, bool_Success, obj_Operation) {
            var obj_Response = obj_Store.proxy.reader.rawData;
            if (bool_Success) {
                this.process_SearchResults(obj_Response);
                obj_This.fireEvent("goodquery", obj_Response.query);
                obj_This.m_obj_Control.enable();
            } else {
                if (obj_Response) {
                    if (obj_Response.errors) {
                        Ext.Msg.alert('Error', "Error - " + obj_Response.errors.msg);
                        this.error_SearchResults(obj_Response.errors.msg);
                    }
                }
            }

            this.m_obj_Mask.hide();
        }, this);

        this.callParent();
        this.on("beforestaterestore", function (obj_This, obj_State, obj_Opts) {
            m_obj_AmpleHarvest_ViewPort.g_obj_RestStateProvider.add_StateObject(this.stateId, this);
        }, this);

    },

    do_Search : function () {
        this.ownerCt.do_Search();
    },

    // Used for loading new information into the screen
    loadURL : function (obj_Params, bool_New) {
        if (obj_Params != null) {
            this.m_obj_Params = obj_Params;
        }
        if (bool_New) {
            this.m_str_CurPage = 0;
        }

        var str_Layout = this.m_obj_Sort.get_Layout();
        if (str_Layout == "ge_grid") {
            this.m_obj_CurTpl = Ext.create('AmpleHarvest.objects.search.template.grid.cGEStandard')
        } else {
            this.m_obj_CurTpl = this.m_int_OutputTPL;
        }

        if (this.m_obj_CurTpl && this.m_obj_CurTpl.m_arr_Fields) {

            this.m_str_PerPage = this.m_obj_CurTpl.perPage;
            this.m_obj_Params.displayFields = Ext.encode(this.m_obj_CurTpl.m_arr_Fields);
        } else {
            alert("No Fields");
        }

        this.m_obj_Params.solrType = this.m_str_SolrType;
        this.m_obj_Params.dataType = this.dataType;
        this.m_obj_ResultsStore.set_ExtraParams(this.m_obj_Params);
        this.m_obj_ResultsStore.pageSize = this.m_str_PerPage;

        // this.m_obj_ResultsStore.load();

        // This loads/refreshes the page
        this.m_obj_ResultsStore.loadPage(1);
    },

    get_SortArray : function () {
        var arr_Output = new Array();
        var obj_Temp = new Object();

        obj_Temp.Field = this.m_obj_Sort.get_Field();
        obj_Temp.Direction = this.m_obj_Sort.get_Direction();

        arr_Output.push(obj_Temp);

        return arr_Output;
    },
    error_SearchResults : function (str_MSG) {
        this.m_obj_Output.removeAll(true);
        this.m_obj_Output.add({
            xtype : 'box',
            html : str_MSG
        })
    },

    process_SearchResults : function (obj_Data) {
        this.m_obj_Output.removeAll(true);

        if (this.m_obj_CurTpl.type == "grid") {
            this.output_LayoutGrid(obj_Data);
        } else {
            this.output_LayoutStandard(obj_Data);
        }
    },
    output_LayoutStandard : function (obj_Data) {

        this.m_str_Query = obj_Data.query;
        this.m_obj_Output.add({
            xtype : 'box',
            html : 'Query - ' + obj_Data.query
        })

        Ext.Array.each(obj_Data.data, function (obj_Doc, int_Index, arr_Docs) {
            // var obj_Size = this.m_obj_Output.getSize();
            // this.m_obj_Output.setSize(obj_Size.width,
            // obj_Size.height
            // + 100);

            obj_Doc.m_str_Query = this.m_str_Query;

            var obj_Article = Ext.create("Ext.panel.Panel", {
                layout : 'fit',
                cls : 'clickable',
                tpl : this.m_obj_CurTpl,
                data : obj_Doc,
                title : obj_Doc.title,
                listeners : {
                    dblclick : {
                        element : 'el', // bind to the
                        // underlying body
                        // property on the
                        // panel
                        fn : function () {
                            m_obj_AmpleHarvest_ViewPort
                                .fireEvent( // TODO: this is not properly accessing the event handler
                                AmpleHarvest.eventHandler.cWindowEvents.C_STR_EVENT_ARTICLE_OPEN, this.id,
                                    this.m_str_Query);
                        },
                        scope : obj_Doc
                    }
                }
            });

            this.m_obj_Output.add(obj_Article);
        }, this);
    },

    output_LayoutGrid : function (obj_Data) {
        var obj_Store = Ext.create('Ext.data.Store', {
            fields : this.m_obj_CurTpl.get_Fields(),
            data : {
                'items' : obj_Data.data
            },
            remoteSort : true,
            proxy : {
                type : 'memory',
                reader : {
                    type : 'json',
                    root : 'items'
                }
            }
        });

        obj_Store.on("datachanged", function (obj_This, obj_Options) {
            this.m_obj_Sort.set_Field(obj_This.sorters.get(0).property);
            this.m_obj_Sort.set_Direction(obj_This.sorters.get(0).direction);
            this.up('ampleharvest-objects-search-panel-csearch').do_Search();
        }, this);

        obj_Store.sorters.add(0, {
            direction : this.m_obj_Sort.get_Direction().toUpperCase(),
            property : this.m_obj_Sort.get_Field()
        })

        this.m_obj_Output.add({
            xtype : 'box',
            html : 'Query - ' + obj_Data.query
        });
        this.m_obj_Output.add(Ext.create('AmpleHarvest.objects.search.results.grid.cGridResults', {
            store : obj_Store,
            columns : this.m_obj_CurTpl.columns
        }));
    },
    activate_Controls : function () {
        this.m_obj_Sort.activate_Controls();
    }
}

);
